package com.wmyg.antifake.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class GetLocationBaiduMap {
    private static Map<String,Object> resultMap =new HashMap<String,Object>();
    private static  String lat="39.822853";
    private static  String lng="116.28506";
    private static final String AK = "7GAxnzcDaWjbiLx19K3Hge1OKBuK5alo";
    private static final String REQURL1 = "http://api.map.baidu.com/geoconv/v1/?coords=";
    private static final String REQURL2 = "&from=1&to=5&ak="+AK;
    private static final String REQURL3 = "http://api.map.baidu.com/geocoder/v2/?ak="+AK+"&location=";
    //String lat="40.512947";
    //String lng="114.881318";
    public static Map getLocationByBaiduMap(String longitude,String latitude) throws Exception {
        String locJson=geturl(REQURL1 + longitude + "," +latitude + REQURL2);
        JSONObject jobject =  JSON.parseObject(locJson);
        JSONArray jsonArray = jobject.getJSONArray("result");
        String lat=jsonArray.getJSONObject(0).getString("y");
        String lng=jsonArray.getJSONObject(0).getString("x");
        String addrJson = geturl(REQURL3 + lat + "," + lng + "&output=json&pois=1");
        JSONObject jobjectaddr =  JSON.parseObject(addrJson);
        resultMap.put("country" , jobjectaddr.getJSONObject(("result")).getJSONObject("addressComponent").getString("country"));
        resultMap.put("province" , jobjectaddr.getJSONObject(("result")).getJSONObject("addressComponent").getString("province"));
        resultMap.put("city" , jobjectaddr.getJSONObject(("result")).getJSONObject("addressComponent").getString("city"));
        resultMap.put("county" , jobjectaddr.getJSONObject(("result")).getJSONObject("addressComponent").getString("district"));
        resultMap.put("details_address",jobjectaddr.getJSONObject(("result")).getString("formatted_address"));
        resultMap.put("longitude",longitude);
        resultMap.put("latitude",latitude);
        return resultMap;
    }
    private static String geturl(String geturl) throws Exception {
        //请求的webservice的url
        URL url = new URL(geturl);
        //创建http链接
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

        //设置请求的方法类型
        httpURLConnection.setRequestMethod("POST");

        //设置请求的内容类型
        httpURLConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");

        //设置发送数据
        httpURLConnection.setDoOutput(true);
        //设置接受数据
        httpURLConnection.setDoInput(true);

        //发送数据,使用输出流
        OutputStream outputStream = httpURLConnection.getOutputStream();
        //发送的soap协议的数据
        //    String requestXmlString = requestXml("北京");

        String content = "user_id="+ URLEncoder.encode("13846", "utf-8");

        //发送数据
        outputStream.write(content.getBytes());

        //接收数据
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = in.readLine()) != null){
            buffer.append(line);
        }
        String str = buffer.toString();

        return str;
    }

}
